package opensocial4gwt;

public final class IdSpec extends BaseJsObject {

	protected IdSpec() {
	}

	public static final class PersonId {
		public static final String VIEWER = "VIEWER";
		public static final String OWNER = "OWNER";
	}

	public static enum GroupId {
		ALL, FRIENDS, SELF;
	}

	private static enum Field {
		GROUP_ID, USER_ID, NETWORK_DISTANCE;

		public native String value() /*-{
			return $wnd.opensocial.IdSpec.Field[this.@opensocial4gwt.IdSpec.Field::name()()];
		}-*/;
	}

	public GroupId getGroupId() {
		String gid = getFieldString(Field.GROUP_ID.value());
		if (gid == null)
			return null;
		else
			return GroupId.valueOf(gid);
	}

	public void setGroupId(GroupId groupId) {
		if (groupId == null)
			setField(Field.GROUP_ID.value(), null);
		else
			setField(Field.GROUP_ID.value(), groupId.name());
	}

	public Integer getNetworkDistance() {
		return getFieldInt(Field.NETWORK_DISTANCE.value());
	}

	public void setNetworkDistance(Integer networkDistance) {
		setField(Field.NETWORK_DISTANCE.value(), networkDistance);
	}

	public String[] getUserIds() {
		return getUserIdsArray().toStringArray();
	}

	private native JsObject getUserIdsArray() /*-{
		var uid = this.getField($wnd.opensocial.IdSpec.Field.USER_ID);
		if (uid == null)
			return [];
		if (typeof uid == 'string')
			return [uid];
		return uid;
	}-*/;

	public void setUserIds(String... uids) {
		if (uids == null)
			setField(Field.USER_ID.value(), null);
		else
			setField(Field.USER_ID.value(), JsObject.newArray(uids));
	}
}
